﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using Lion.WeiXin.Core.Models;
using Lion.WeiXin.Tools.Log;
namespace Lion.WeiXin.Sms
{
    public class SendSMS :ISendSms
    {
        string v_smsUserId;
        string v_SmsPwd;
        string v_SmsSign;

        public SendSMS()
        {
            this.v_smsUserId = Config.SmsUserID;
            this.v_SmsPwd = Config.SmsPwd;
            this.v_SmsSign = Config.SmsSign;
        }

        /// <summary>
        /// 以 HTTP 的 POST 提交方式 发送短信(ASP.NET的网页或是C#的窗体，均可使用该方法)
        /// </summary>
        /// <param name="mobile">要发送的手机号码</param>
        /// <param name="msg">要发送的内容</param>
        /// <returns>发送结果</returns>
        public string SendMsg(string mobile, string msg)
        {
            if (!Config.IsOpenSMS)
            {
                LogHelper.Info("短信发送配置为OFF，不发送短信。要下发短信请设置IsOpenSMS 配置为 ON。");
            }

            string name = this.v_smsUserId;
            //登陆web平台 http://web.cr6868.com  在管理中心--基本资料--接口密码（28位） 如登陆密码修改，接口密码会发生改变，请及时修改程序
            string pwd = this.v_SmsPwd;
            //一般为企业简称
            string sign = this.v_SmsSign;            
            StringBuilder arge = new StringBuilder();

            arge.AppendFormat("name={0}", name);
            arge.AppendFormat("&pwd={0}", pwd);
            arge.AppendFormat("&content={0}", msg);
            arge.AppendFormat("&mobile={0}", mobile);
            arge.AppendFormat("&sign={0}", sign);
            arge.Append("&type=pt");
            string weburl = "http://web.cr6868.com/asmx/smsservice.aspx";

            string resp = PushToWeb(weburl, arge.ToString(), Encoding.UTF8);
            if (resp.Split(',')[0] == "0")
            {
                //提交成功
                LogHelper.Info("短信提交成功！ 返回状态码:{0}", resp);
            }
            else
            {
                //提交失败，可能余额不足，或者敏感词汇等等
                LogHelper.Info("短信提交失败！ 返回状态码:{0}", resp);
            }

            return resp;//是一串 以逗号隔开的字符串。阅读文档查看响应的意思
        }

       

        /// <summary>
        /// HTTP POST方式
        /// </summary>
        /// <param name="weburl">POST到的网址</param>
        /// <param name="data">POST的参数及参数值</param>
        /// <param name="encode">编码方式</param>
        /// <returns></returns>
        public string PushToWeb(string weburl, string data, Encoding encode)
        {
            byte[] byteArray = encode.GetBytes(data);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(weburl));
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();

            //接收返回信息：
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader aspx = new StreamReader(response.GetResponseStream(), encode);
            return aspx.ReadToEnd();
        }
    }
}